<?php
include 'utilities.php';
include 'crawl.php';
Utititles::includeClasses();

$crawl = new Crawl();
$tb_sources = new Tb_Ratio_Sources();
$tb_games = new Tb_Games();
$tb_club = new Tb_Clubs();
/*
<td.*?>[\n\s\t\r]*<p.*?>(.*?)<\/p>[\n\s\t\r]*<\/td>
<td.*?>[\n\s\t\r]*<p.*?<font.*?>(.*?)<\/font>.*?<\/p>[\n\s\t\r]*<p.*?<font.*?>(.*?)<\/font>.*?<\/p>[\n\s\t\r]*<\/td>
<td.*?>[\n\s\t\r]*<p.*?>(.*?)<\/p>[\n\s\t\r]*<p.*?>(.*?)<\/p>[\n\s\t\r]*<\/td>
<td.*?>[\n\s\t\r]*<p.*?><font.*?>(.*?)<\/font><span.*?>(.*?)<\/span><\/p>[\n\s\t\r]*<p.*?><font.*?>(.*?)<\/font><span.*?>(.*?)<\/span><\/p>[\n\s\t\r]*<\/td>
<td.*?>[\n\s\t\r]*<p.*?>(.*?)<\/p>[\n\s\t\r]*<p.*?>(.*?)<\/p>[\n\s\t\r]*<p.*?>(.*?)<\/p>[\n\s\t\r]*<\/td>
<td.*?>[\n\s\t\r]*<a.*?href=\"(.*?)\".*?<\/a>.*?<a.*?href=\"(.*?)\".*?<\/a>.*?<a.*?href=\"(.*?)\".*?<\/a>[\n\s\t\r]*<\/td>
*/
$ratios = $tb_sources->get();

// Crawl
foreach ($ratios as $ratio){
    for ($i = $ratio['start']; $i <= $ratio['end']; $i++) {
        $url = vsprintf($ratio['url'], array($i, $i));
        $data = $crawl->getPage($url);
        $matches = $crawl->parseData($data, $ratio['pattern']);
        //1: date 21/08/2012 02:00
        //2: First club Arsenal
        //3: Second club Sunderland
        //4: First rate club
        //5: Second rate club
        if ($matches[1]) {
            $index = 0;
            foreach ($matches[1] as $val) {
                // Date
                $datetime = DateTime::createFromFormat( 'd/m/Y H:i', $val);
                $timestamp = $datetime->getTimestamp();

                // First name club
                $name1 = $matches[2][$index];
                $club1 = $tb_club->get($name1);
                // Second name club
                $name2 = $matches[3][$index];
                $club2 = $tb_club->get($name2);
                // Rate
                $rate_1 = 0;
                $rate_2 = 0;
                $mon_1 = 0;
                $mon_2 = 0;
                $matches_rate = $crawl->parseData($matches[4][$index], $ratio['pattern_1']);
                if (!empty($matches_rate[1])) {
                    $rate_2 = $matches_rate[1][0];
                    $mon_1 = preg_replace('/<.*?>/', '', $matches_rate[2][0]);

                    $matches_rate_temp = $crawl->parseData($matches[5][$index], $ratio['pattern_2']);
                    $rate_1 = 0;
                    $mon_2 = preg_replace('/<.*?>/', '', $matches_rate_temp[1][0]);
                } else {
                    $matches_rate_temp = $crawl->parseData($matches[4][$index], $ratio['pattern_2']);
                    $rate_2 = 0;
                    $mon_1 =  preg_replace('/<.*?>/', '', $matches_rate_temp[1][0]);

                    $matches_rate_temp = $crawl->parseData($matches[5][$index], $ratio['pattern_1']);
                    $rate_1 = $matches_rate_temp[1][0];
                    $mon_2 = preg_replace('/<.*?>/', '', $matches_rate_temp[2][0]);
                }
                if ((!empty($club1['id'])) && (!empty($club2['id']))) {
                    $matches[7][$index] = preg_replace('/<.*?>/', '', $matches[7][$index]);
                    $matches[9][$index] = preg_replace('/<.*?>/', '', $matches[9][$index]);
                    $params = array(
                        'date' => $timestamp,
                        'id_club1' => $club1['id'],
                        'id_club2' => $club2['id'],
                        'club1_rate' => $rate_1,
                        'club2_rate' => $rate_2,
                        'club1_mon' => $mon_1,
                        'club2_mon' => $mon_2,
                        'rscores' => $matches[6][$index],
                        'mo'    =>  $matches[7][$index]+0.0,
                        'mu'    =>  $matches[9][$index]+0.0,
                        'mwin'    =>  $matches[10][$index]+0.0,
                        'mblance'    =>  $matches[11][$index]+0.0,
                        'mlost'    =>  $matches[12][$index]+0.0,
                        'url_asian' =>  $ratio['source'].$matches[13][$index],
                        'url_ou' =>  $ratio['source'].$matches[14][$index],
                        'url_europe' =>  $ratio['source'].$matches[15][$index]
                    );                    
                    $tb_games->update($params);
                }
                //$tb_sources->updateStatus($ratio['id']);
                $index++;
                echo $index."\n";
            }
        }
    }
}